class NetResult {
    String count;
    String start;
    String total;
    List<Movie> subjects; //注意这个自定义对象列表的属性的解析过程
    
    NetResult.cjz(Map map) {
        //命名构造函数
        count = map["count"].toString();
        start = map["start"].toString();
        //subjects = map["subjects"]; 这样写将直接抛异常,因为你试图将List<dynamic>转List<Movie>
        subjects = parseJsonMap(map);
    }
    
    static List<Movie> parseJsonMap(Map json) {
        var list = json['subjects'] as List; //list 的类型是 List<dynamic>
        List<Movie> movieList = list.map((index) {
            return Movie.fromJsonMap(index); //遍历list，将dynamic类型数据调用Movie的构造函数生成Movie对象，在不是=>简写的过程中，这里千万记得关键字return
        }).toList();

//遍历list的简写形式，推荐下面这一行代码的这种遍历方式
//List<Movie> movieList = list.map((index)=>Movie.fromJsonMap(index)).toList();
        
        return movieList;
    }
}

class Movie {
    String id;
    String title;
    String year;
    String originalTitle;
    
    List<String> genres; //注意这个原始类型列表的属性的解析过程
    Rating rating;
    CoverImage images;
    
    //fromJsonMap这个名字太正式了，初学者以为是dart语言自带的解析方式，实际上这个名字任意取，解析过程得手写。
    Movie.fromJsonMap(Map map) //命名构造函数的书写方式之二分号,初始化属性列表
        : id = map["id"].toString(),
            title = map["title"],
            originalTitle = map["original_title"],
    // genres = map["genres"], 这样写将抛异常
            genres = new List<String>.from(map["genres"]),
    //这里必须显式的将List<dynamic>强转为List<String>
            rating = Rating.x(map["rating"]),
            images = CoverImage.cjz(map["images"]){
        //这里空的构造函数体 可以直接用分号代替;
        
    }
}

class Rating {
    String max;
    String average;
    String stars;
    String min;
    
    Rating.x(Map map)
        : max = map["max"].toString(),
            average = map["average"].toString(),
            stars = map["stars"].toString(),
            min = map["min"].toString();
}

class CoverImage {
    String small;
    String large;
    String medium;
    
    CoverImage({this.small, this.large, this.medium}); //构造函数的大括号代表可选参数
    
    factory CoverImage.cjz(Map<String, dynamic> map) {
        //工厂构造函数，对比其他几种写法。
        return CoverImage(
            small: map["small"], //
            large: map["large"], //
            medium: map["medium"] //
        );
    }
}

//豆瓣Top250接口
// http://api.douban.com/v2/movie/top250?start=0&count=25
//返回的数据格式如下：
// {
//     "rating": {
//       "max": 10,
//       "average": 9.6,
//       "stars": "50",
//       "min": 0
//     },
//     "genres": [
//       "犯罪",
//       "剧情"
//     ],
//     "title": "肖申克的救赎",
//     "casts": [
//       {
//         "alt": "https://movie.douban.com/celebrity/1054521/",
//         "avatars": {
//           "small": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.webp",
//           "large": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.webp",
//           "medium": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.webp"
//         },
//         "name": "蒂姆·罗宾斯",
//         "id": "1054521"
//       },
//       {
//         "alt": "https://movie.douban.com/celebrity/1054534/",
//         "avatars": {
//           "small": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34642.webp",
//           "large": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34642.webp",
//           "medium": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34642.webp"
//         },
//         "name": "摩根·弗里曼",
//         "id": "1054534"
//       },
//       {
//         "alt": "https://movie.douban.com/celebrity/1041179/",
//         "avatars": {
//           "small": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p5837.webp",
//           "large": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p5837.webp",
//           "medium": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p5837.webp"
//         },
//         "name": "鲍勃·冈顿",
//         "id": "1041179"
//       }
//     ],
//     "collect_count": 1575715,
//     "original_title": "The Shawshank Redemption",
//     "subtype": "movie",
//     "directors": [
//       {
//         "alt": "https://movie.douban.com/celebrity/1047973/",
//         "avatars": {
//           "small": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.webp",
//           "large": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.webp",
//           "medium": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.webp"
//         },
//         "name": "弗兰克·德拉邦特",
//         "id": "1047973"
//       }
//     ],
//     "year": "1994",
//     "images": {
//       "small": "http://img1.doubanio.com/view/photo/s_ratio_poster/public/p480747492.webp",
//       "large": "http://img1.doubanio.com/view/photo/s_ratio_poster/public/p480747492.webp",
//       "medium": "http://img1.doubanio.com/view/photo/s_ratio_poster/public/p480747492.webp"
//     },
//     "alt": "https://movie.douban.com/subject/1292052/",
//     "id": "1292052"
//   }
